package com.ruoyi.agentExpend.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.agent.domain.SysAgent;
import com.ruoyi.agent.mapper.SysAgentMapper;
import com.ruoyi.agentExpend.mapper.AgentExpendMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.order.domain.SysOrderOne;
import com.ruoyi.order.mapper.SysOrderOneMapper;
import com.ruoyi.packages.domain.SysPackage;
import com.ruoyi.packages.mapper.SysPackageMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.agentExpend.domain.AgentExpend;
import com.ruoyi.agentExpend.service.IAgentExpendService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代理商消费明细Controller
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/agentExpend/agentExpend")
public class AgentExpendController extends BaseController
{
    @Autowired
    private IAgentExpendService agentExpendService;

    @Autowired
    private AgentExpendMapper agentExpendMapper;

    @Autowired
    private SysAgentMapper agentMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysOrderOneMapper orderOneMapper;

    @Autowired
    private SysPackageMapper packageMapper;

    @Autowired
    private SysDeptMapper deptMapper;

//    @Autowired
//    private ISysUserService userService;

    /**
     * 查询代理商消费明细列表
     */
//    @PreAuthorize("@ss.hasPermi('agentExpend:agentExpend:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgentExpend agentExpend,@RequestParam(name = "params[beginCreateTime]", required = false) Date beginCreateTime,
                           @RequestParam(name = "params[endCreateTime]", required = false) Date endCreateTime)
    {

        //todo 724写这
        List<AgentExpend> list = null;
//        if (CollectionUtils.isEmpty(list) || list.size()<=0) {
//           return success(list);
//        }
        Long deptId = SecurityUtils.getDeptId();
        String type = deptMapper.selectDeptById(deptId).getType();

        if (!type.equals("0") && !type.equals("1")) {
            throw new ServiceException("您没有权限操作");
        }
        if (beginCreateTime!=null) {
            agentExpend.setBeginCreateTime(beginCreateTime);
        }

        if (endCreateTime!=null) {
            agentExpend.setEndCreateTime(endCreateTime);
        }


        if (deptId.equals(100l)) {
            //说明是管理端查询代理商的明细：返回所有的代理商消费明细：

             startPage();
              list = agentExpendService.selectAgentExpendList(agentExpend);
        }
        else {
            //说明是代理商查询自己的消费明细：
            //先查询对应的部门存在代理商吗
            List<SysAgent> agents = agentMapper.selectSysAgentByDeptId(deptId);
            if (CollectionUtils.isEmpty(agents)) {
                throw new ServiceException("代理商信息有误");
            }
            agentExpend.setAgentId(agents.get(0).getId());
            startPage();
            list = agentExpendService.selectAgentExpendList(agentExpend);

        }

        return getDataTable(list);
    }

    /**
     * 导出代理商消费明细列表
     */
    @PreAuthorize("@ss.hasPermi('agentExpend:agentExpend:export')")
    @Log(title = "代理商消费明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgentExpend agentExpend)
    {
        List<AgentExpend> list = agentExpendService.selectAgentExpendList(agentExpend);
        ExcelUtil<AgentExpend> util = new ExcelUtil<AgentExpend>(AgentExpend.class);
        util.exportExcel(response, list, "代理商消费明细数据");
    }

    /**
     * 获取代理商消费明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('agentExpend:agentExpend:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(agentExpendService.selectAgentExpendById(id));
    }

    /**
     * 新增代理商消费明细
     */
    @PreAuthorize("@ss.hasPermi('agentExpend:agentExpend:add')")
    @Log(title = "代理商消费明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgentExpend agentExpend)
    {
        return toAjax(agentExpendService.insertAgentExpend(agentExpend));
    }

    /**
     * 修改代理商消费明细
     */
    @PreAuthorize("@ss.hasPermi('agentExpend:agentExpend:edit')")
    @Log(title = "代理商消费明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgentExpend agentExpend)
    {
        return toAjax(agentExpendService.updateAgentExpend(agentExpend));
    }

    /**
     * 删除代理商消费明细
     */
    @PreAuthorize("@ss.hasPermi('agentExpend:agentExpend:remove')")
    @Log(title = "代理商消费明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(agentExpendService.deleteAgentExpendByIds(ids));
    }
}
